package cn.tedu.cwgl.controller;

import cn.tedu.cwgl.entity.House;
import cn.tedu.cwgl.entity.Item;
import cn.tedu.cwgl.entity.UserInfo;
import cn.tedu.cwgl.service.MoneyManageService;
import cn.tedu.cwgl.util.Config;
import cn.tedu.cwgl.util.PageModel;
import cn.tedu.cwgl.util.Result;
import cn.tedu.cwgl.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class MoneymanageController {
    @Autowired
    MoneyManageService moneyManageService;

    @RequestMapping("/moneymanage/getBillsByWhere/{pageNo}/{pageSize}")
    @ResponseBody
    public Result<Item> moneymanage(Item item,
                                    @PathVariable int pageNo, @PathVariable int pageSize, HttpSession session) {
        item = getItem(item, session);
        UserInfo userInfo = (UserInfo) session.getAttribute(Config.CURRENT_USERNAME);
        if (userInfo.getRoleid() == 2) {
            item.setHouseid(userInfo.getHouseid());
        }
        if (userInfo.getRoleid() == 3) {
            item.setId(userInfo.getId());
        }
        PageModel model = new PageModel<>(pageNo, item);
        model.setPageSize(pageSize);
        Result<Item> item2 = moneyManageService.findMoneyManage(model);
        return item2;
    }

    private Item getItem(Item item, HttpSession session) {
        UserInfo user = Config.getSessionUser(session);
        // 当登录用户为家主时，查询默认的是全家信息
        // 当登录用户为普通用户时，仅仅查询当前用户
        if (user.getRoleid() == 2) {
            item.setHouseid(user.getHouseid());
        } else if (user.getRoleid() == 3) {
            item.setUserid(user.getId());
        }
        return item;
    }

    @RequestMapping("/moneymanage/updateBill")
    @ResponseBody
    public Result updateMon(Item item) {

        int updateMon = moneyManageService.updateMon(item);
        if (updateMon > 0) {
            return ResultUtil.success();
        }
        return ResultUtil.unSuccess();
    }

    @RequestMapping("/moneymanage/delBill")
    @ResponseBody
    public Result dell(int id, House house, Item item) {
        item.setId(null);
        PageModel model = new PageModel<>(10, item);
        model.setPageSize(20);
//        删除moneyManage表的时候会涉及到对两个表同时删除，由于外键限制，需要先删除moneyManage表，后删除house表
//        删除house表，是通过id删除，而house表的id是与moneyManage表的userid关联在一起的，所以执行删除之前，先将
//        house表的id存起来。
        Result<Item> moneyManage = moneyManageService.findMoneyManage(model);
        List<Item> datas = moneyManage.getDatas();
        for (Item data : datas) {
            if (data.getId() == id) {
                house.setId(data.getUserid());
            }
        }
        int houseid = house.getId();
        int delect = 0;
        try {
            delect = moneyManageService.delect(id, houseid);
        } catch (Exception e) {
            e.printStackTrace();
        }
//        moneyManageService.delectAfter(house.getId());
        if (delect > 0) {
            return ResultUtil.success();
        }
        return ResultUtil.unSuccess();
    }

    @RequestMapping("/moneymanage/addBill")
    @ResponseBody
    public Result add(Item item, HttpSession session, House house) {
        UserInfo user = Config.getSessionUser(session);
        house.setOwnerid(user.getId());
        int house_id = moneyManageService.addMonBefore(house);
        item.setUserid(house_id);
        item.setHouseid(user.getHouseid());
        int addMon = moneyManageService.addMon(item);
        if (addMon > 0) {
            return ResultUtil.success();
        }
        return ResultUtil.unSuccess();
    }
}
